/**
 * 作者: hwp
 * 日期: 2025-05-15 17:43:34
 * 备注: 联盟礼盒宝箱列表
 */
package com.motu.monstercity.server.game.userdata;

import com.motu.monstercity.protocol.Common.PBUnionGiftBoxList;
import com.motu.vertx.module.utility.model.BaseModel;
import com.motu.vertx.module.utility.model.Model;
import io.vertx.core.json.JsonObject;
import io.vertx.core.json.JsonArray;

public class UnionGiftBoxListModel extends BaseModel implements Model {
    public static final String LISTKEY = "union_id";
    public static final int REDIS_EXPIRE_TIME = 0;// redis过期时间
    public static final int UPDATE_DB_TYPE = UPDATE_DB_TYPE_QUEUE;// 数据入库类型 队列入库
    public static final boolean ORDER_LIST = false;// 是否需要额外的有序列表

    protected long id; // id
    protected long unionId; // 联盟ID
    protected long userId; // 成员玩家id (提供该礼包的玩家)
    protected int type; // 礼盒类型 (1-活跃礼盒； 2-盟友赠礼)
    protected long activateTime; // 激活时间 (只保留24小时)
    protected String reward = ""; // 奖励
    protected String param = ""; // 参数 (活跃值或者礼包Id)

    public void reset() {
    }

    protected String getKey() {
        return "UnionGiftBoxList." + id;
    }

    protected String getListKey() {
        return "UnionGiftBoxList." + unionId;
    }

    protected String getListItemKey() {
        return id + "";
    }

    public void doCreate() {
        setTableId();
        insert();
    }

    public void setTableId() {
        putId(incrTableId("union_gift_box_list"));
    }

    public JsonObject insert() {
        return super.insert(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, LISTKEY, getListKey(), getListItemKey(), ORDER_LIST);
    }

    public void delete() {
        super.delete(getKey(), id, UPDATE_DB_TYPE, 0, LISTKEY, getListKey(), getListItemKey(), ORDER_LIST);
    }

    /**
     * 根据入库类型更新
     */
    public JsonObject update() {
        return super.update(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, 0, getAllObj(), LISTKEY, getListKey(), getListItemKey());
    }

    /**
     * 实时入队列
     */
    public JsonObject updateQueueNow() {
        return super.update(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, 0, getAllObj(), LISTKEY, getListKey(), getListItemKey());
    }

    public JsonObject getAllObj(){
        JsonObject obj = new JsonObject();
        obj.put("id", id); // id
        obj.put("unionId", unionId); // 联盟ID
        obj.put("userId", userId); // 成员玩家id (提供该礼包的玩家)
        obj.put("type", type); // 礼盒类型 (1-活跃礼盒； 2-盟友赠礼)
        obj.put("activateTime", activateTime); // 激活时间 (只保留24小时)
        obj.put("reward", reward); // 奖励
        obj.put("param", param); // 参数 (活跃值或者礼包Id)
        return obj;
    }

    public PBUnionGiftBoxList.Builder takeInitBuilder() {
        PBUnionGiftBoxList.Builder sb = PBUnionGiftBoxList.newBuilder();
        if (id != 0) {
            sb.setId(id);
        }
        if (type != 0) {
            sb.setType(type);
        }
        if (activateTime != 0) {
            sb.setActivateTime(activateTime);
        }
        if (!reward.isEmpty()) {
            sb.setReward(reward);
        }
        if (!param.isEmpty()) {
            sb.setParam(param);
        }
        return sb;
    }

    public PBUnionGiftBoxList.Builder takeBuilder() {
        if (updateObj == null || updateObj.isEmpty()) {
            return takeInitBuilder();
        }
        PBUnionGiftBoxList.Builder sb = PBUnionGiftBoxList.newBuilder();
        if (isFieldUpdate("id")) {
            sb.setId(id);
        }
        if (isFieldUpdate("type")) {
            sb.setType(type);
        }
        if (isFieldUpdate("activateTime")) {
            sb.setActivateTime(activateTime);
        }
        if (isFieldUpdate("reward")) {
            sb.setReward(reward);
        }
        if (isFieldUpdate("param")) {
            sb.setParam(param);
        }
        return sb;
    }

    public long getId() {
        return id;
    }

    public void putId(long id) {
        if (this.id != id) {
            this.id = id;
            updateRedisHashField(getKey(), "id", this.id);
        }
    }

    public void addId(long value) {
        this.id += value;
        updateRedisHashField(getKey(), "id", this.id);
    }

    public long getUnionId() {
        return unionId;
    }

    public void putUnionId(long unionId) {
        if (this.unionId != unionId) {
            this.unionId = unionId;
            updateRedisHashField(getKey(), "unionId", this.unionId);
        }
    }

    public void addUnionId(long value) {
        this.unionId += value;
        updateRedisHashField(getKey(), "unionId", this.unionId);
    }

    public long getUserId() {
        return userId;
    }

    public void putUserId(long userId) {
        if (this.userId != userId) {
            this.userId = userId;
            updateRedisHashField(getKey(), "userId", this.userId);
        }
    }

    public void addUserId(long value) {
        this.userId += value;
        updateRedisHashField(getKey(), "userId", this.userId);
    }

    public int getType() {
        return type;
    }

    public void putType(int type) {
        if (this.type != type) {
            this.type = type;
            updateRedisHashField(getKey(), "type", this.type);
        }
    }

    public void addType(int value) {
        this.type += value;
        updateRedisHashField(getKey(), "type", this.type);
    }

    public long getActivateTime() {
        return activateTime;
    }

    public void putActivateTime(long activateTime) {
        if (this.activateTime != activateTime) {
            this.activateTime = activateTime;
            updateRedisHashField(getKey(), "activateTime", this.activateTime);
        }
    }

    public void addActivateTime(long value) {
        this.activateTime += value;
        updateRedisHashField(getKey(), "activateTime", this.activateTime);
    }

    public String getReward() {
        return reward;
    }

    public void putReward(String reward) {
        if (!this.reward.equals(reward)) {
            this.reward = reward;
            updateRedisHashField(getKey(), "reward", this.reward);
        }
    }

    public String getParam() {
        return param;
    }

    public void putParam(String param) {
        if (!this.param.equals(param)) {
            this.param = param;
            updateRedisHashField(getKey(), "param", this.param);
        }
    }

}